import { getSignature } from '../../services/user';

var app = getApp()

Page({
  data: {
    name: '',
    idcard: '',
    userFrontImage: '',
    userBehindImage: '',
    userAllImage: ''
  },
  onShow() {
    my.setNavigationBar({
      title: '实名认证',
      backgroundColor: '#4BA3F9'
    })
  },
  inputname(e) {
    this.setData({
      name: e.detail.value,
    });
  },
  inputidcard(e) {
    this.setData({
      idcard: e.detail.value,
    });
  },
  onSubmit(e) {
    if (this.data.name.length <= 0) {
      my.showToast({
        type: 'fail',
        content: '姓名不能为空',
        duration: 2000,
      });
      return false
    } if (this.data.idcard.length <= 0) {
      my.showToast({
        type: 'fail',
        content: '身份证不能为空',
        duration: 2000,
      });
      return false
    }
    if (this.data.userFrontImage.length <= 0) {
      my.showToast({
        type: 'fail',
        content: '身份证正面不能为空',
        duration: 2000,
      });
      return false
    }
    if (this.data.userBehindImage.length <= 0) {
      my.showToast({
        type: 'fail',
        content: '身份证背面不能为空',
        duration: 2000,
      });
      return false
    }

    let param = {
      userName: this.data.name,
      userIdCard: this.data.idcard,
      userFrontImage: this.data.userFrontImage,
      userBehindImage: this.data.userBehindImage,
    }
    
    /**
     * TODO::实名数据验证
     */
  },
  async uploadFile(type) {
    my.chooseImage({
      chooseImage: 1,
      success: async res => {
        const path = res.apFilePaths[0];
        console.log('image res', path)

        const { data: signData, code } = await getSignature()
        if (code != 0) return false

        const fileKey = `rental/user/${signData.key}`
        const ossHost = app.globalData.ossHost

        my.uploadFile({
          url: ossHost,
          fileType: 'image',
          fileName: 'file',
          filePath: path,
          formData: {
            key: fileKey,
            policy: signData.policy,
            OSSAccessKeyId: signData.OSSAccessKeyId,
            success_action_status: signData.success_action_status,
            signature: signData.signature
          },
          header: { "Content-Type": "multipart/form-data" },
          success: res => {
            if (res.statusCode == "200") {
              if (type == 1) {
                this.setData({ userFrontImage: `${ossHost}/${fileKey}` })
              } else if (type == 2) {
                this.setData({ userBehindImage: `${ossHost}/${fileKey}` })
              }
            } else {
              my.showToast({
                type: 'fail',
                content: '上传照片失败',
                duration: 2000,
              })
            }
          },
          fail: function (res) {
            my.showToast({
              type: 'fail',
              content: '上传照片失败',
              duration: 2000,
            })
          },
        });
      },
    });
  },
  uploadFile1() {
    this.uploadFile(1)
  },
  uploadFile2() {
    this.uploadFile(2)
  },
});
